<?php
/**
 * @Author:      zk
 * @DateTime:    18-9-7 下午1:20
 * @Description: 用于 ASS 系统供货商管理
 */
class ASSSupplierController extends BaseController {
    
    /**
     * @Author:          zk
     * @DateTime:        18-9-7 下午1:25
     * @Description:     获取所有门店列表
     * @param string $token             用户token
     * @link http://api.anhui.com/ass/supply/getShopList?token=60fccf4c8de5a9fc2f00d072cc89c941
     */
    public function getShopList(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get('ass_error.args_error_100000');
                $response['result'] = $validator->messages()->all();
                break;
            }
            
            $res = $this->verificationASSUserToken(Input::get('token'),$response);
            if(is_null($res)){
                break;
            }
            
            $shops_info = MaintenanceShops::getAllShopInfo();   //获取所有有效的门店信息
            $response = [
                'code' => 0, 
                'msg' => 'ok',
                'result' => [
                    'list' => $shops_info,
                ],
            ];
        }while(false);
        
        Log::info('info', array('system' => 'ass' ,'event' => '/supply/getShopList', 'input' =>Input::all() ,'output' => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:          zk
     * @DateTime:        18-9-7 下午4:19
     * @Description:     获取供应商供应耗材统计列表 | 供货情况统计
     * @param string $token             用户token
     * @param string $start_date        开始日期
     * @param string $end_date          结束日期
     * @param int $shop_id              门店ID
     * @param string $code              商品编码
     * @param string $name              配件名称
     * @param int $storage_way          状态  1:签收入库;2:出库退回;
     * @param int $page                 页码 [ 默认为1 ]
     * @param int $page_size            每页数据条数 [ 默认为20 ]
     * @link http://api.anhui.com/ass/supply/getSupplyItemList?token=60fccf4c8de5a9fc2f00d072cc89c941
     */
    public function getSupplyItemList(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get('ass_error.args_error_100000');
                $response['result'] = $validator->messages()->all();
                break;
            }
            
            $res = $this->verificationASSUserToken(Input::get('token'),$response);
            if(is_null($res)){
                break;
            }
            
            $supplier_id    = $res->id;                         //供应商ID
            $start_date     = Input::get('start_date', '');     //开始日期
            $end_date       = Input::get('end_date', '');       //结束日期
            $shop_id        = Input::get('shop_id', '');        //门店ID
            $code           = Input::get('code', '');           //商品编码
            $name           = Input::get('name', '');           //配件名称
            $storage_way    = Input::get('storage_way', '');    //状态  1:签收入库;2:出库退回;
            $page           = Input::get('page', 1);            //页码 [ 默认为1 ]
            $page_size      = Input::get('page_size', 20);      //每页数据条数 [ 默认为20 ]
            $final_array    = [];
            
            //step 1 签收入库 start
            //maintenance_storage_item.name, 
            $storage_list_select = 'maintenance_storage_item.list_id, maintenance_storage_item.type, '
                    . 'maintenance_storage_item.item_type, maintenance_storage_item.item_id, maintenance_storage_item.code, '
                    . 'maintenance_storage_item.number, maintenance_storage_item.tax_purchase_price, '
                    . 'maintenance_storage_item.tax_purchase_subtotal, maintenance_storage_item.data_id, '
                    . 'maintenance_storage_item.no_tax_purchase_price, '
                    . 'maintenance_storage_list.date, maintenance_storage_list.shop_id, maintenance_storage_list.team, '
                    . 'maintenance_storage_list.storage_type, maintenance_storage_list.type storage_list_type, '
                    . 'maintenance_storage_list.pay_type ,maintenance_storage_list.cust_id, '
                    . 'maintenance_shops.name AS shop_name, '
                    . 'sc_user.real_name, supply_item.name';
            $storage_list = MaintenanceStorageItem::leftJoin('maintenance_storage_list', 'maintenance_storage_item.list_id', '=', 'maintenance_storage_list.id')
                    ->leftJoin('sc_user', 'maintenance_storage_list.cust_id', '=', 'sc_user.id')
                    ->leftJoin('maintenance_shops', 'maintenance_shops.id', '=', 'maintenance_storage_item.shop_id')
                    ->leftJoin('supply_item', 'maintenance_storage_item.item_id', '=', 'supply_item.id')
                    ->where('maintenance_storage_list.supplier', $supplier_id)
                    ->where('maintenance_storage_item.status', 1)
                    ->where('maintenance_storage_list.status', 1)
                    ->where('maintenance_storage_list.type', 1)
                    ->orderBy('maintenance_storage_list.implement_time', 'desc')
                    //->whereIn('maintenance_storage_list.type', [1, 2])
                    ->selectRaw($storage_list_select);
            if($start_date != ''){
                $storage_list->where('maintenance_storage_list.implement_time', '>=', $start_date);
            }
            if($end_date != ''){
                $storage_list->where('maintenance_storage_list.implement_time', '<=', $end_date);
            }
            if($code != ''){
                $storage_list->where('maintenance_storage_item.code', 'like', '%'.$code.'%');
            }
            if($name != ''){
                $storage_list->where('maintenance_storage_item.name', 'like', '%'.$name.'%');
            }
            if($shop_id != ''){
                $storage_list->where('maintenance_storage_list.shop_id', $shop_id);
            }
            $storage_list = $storage_list->get()->toArray();
            if(($storage_way != 2) && !empty($storage_list)){
                foreach ($storage_list as $v) {
                    $temp = [
                        'storage_list_id' => $v['list_id'], //单号
                        'list_type' => '签收入库',
                        'date' => $v['date'], //日期
                        'code' => $v['code'], //商品编码
                        'name' => $v['name'], //商品名称
                        'number' => $v['number'], //数量
                        'tax_purchase_price' => $v['tax_purchase_price'], //含税进价
                        'tax_purchase_subtotal' => $v['tax_purchase_subtotal'], //含税小计
                        'tax_rate' => $v['tax_purchase_price']==0 ? 0 : ($v['tax_purchase_price'] * $v['number'])/$v['tax_purchase_subtotal'],
                        'storage_type' => $v['storage_type'], //入库方式
                        'no_tax_purchase_price' => $v['no_tax_purchase_price'], //无税进价
                        'no_tax_purchase_subtotal' => $v['no_tax_purchase_price'] * $v['number'], //无税小计
                        'pay_type' => $v['pay_type'], //付款方式
                        'type' => $v['type'], //入库类型
                        'item_type' => $v['item_type'], //耗材类型
                        'item_id' => $v['item_id'], //耗材id
                        'storage_list_type' => $v['storage_list_type'], //入库类型
                        'shop_id' => $v['shop_id'],
                        'team_id' => $v['team'],
                        'shop_name' => $v['shop_name'],
                        'real_name' => $v['real_name'],
                    ];
                    array_push($final_array, $temp);
                }
            }
            //step 1 签收入库 end
            
            //step 2 出库退回 start
            //maintenance_out_item.name,
            $out_list_select = 'maintenance_storage_item.type, '
                    . 'maintenance_out_item.list_id, maintenance_out_item.item_type, '
                    . 'maintenance_out_item.item_id, maintenance_out_item.code,  '
                    . 'maintenance_out_item.number, maintenance_out_item.tax_purchase_price, '
                    . 'maintenance_out_item.tax_purchase_subtotal, maintenance_out_item.data_id, '
                    . 'maintenance_out_item.no_tax_purchase_price, '
                    . 'maintenance_out_list.date, maintenance_out_list.shop_id, maintenance_out_list.team, maintenance_out_list.pay_type, '
                    . 'maintenance_storage_list.storage_type , maintenance_storage_list.type storage_list_type, '
                    . 'maintenance_shops.name AS shop_name, '
                    . 'sc_user.real_name, supply_item.name';
            //maintenance_out_item
            $out_list = MaintenanceOutItem::leftJoin('maintenance_out_list', 'maintenance_out_item.list_id', '=', 'maintenance_out_list.id')
                    ->leftJoin('maintenance_storage_item', 'maintenance_storage_item.id', '=', 'maintenance_out_item.storage_id')
                    ->leftJoin('maintenance_storage_list', 'maintenance_storage_list.id', '=', 'maintenance_out_item.storage_list_id')
                    ->leftJoin('sc_user', 'maintenance_out_list.cust_id', '=', 'sc_user.id')
                    ->leftJoin('maintenance_shops', 'maintenance_shops.id', '=', 'maintenance_storage_item.shop_id')
                    ->leftJoin('supply_item', 'maintenance_out_item.item_id', '=', 'supply_item.id')
                    ->where('maintenance_out_list.consuming_id', $supplier_id)
                    ->where('maintenance_out_list.status', 1)
                    ->where('maintenance_out_item.status', 1)
                    ->where('maintenance_out_list.type', 2)
                    ->where('maintenance_out_list.out_type', 2)
                    ->orderBy('maintenance_out_list.implement_time', 'desc')
                    ->selectRaw($out_list_select);
            if($start_date != ''){
                $out_list->where('maintenance_out_list.implement_time', '>=', $start_date);
            }
            if($end_date != ''){
                $out_list->where('maintenance_out_list.implement_time', '<=', $end_date);
            }
            if($code != ''){
                $out_list->where('maintenance_out_item.code', 'like', '%'.$code.'%');
            }
            if($name != ''){
                $out_list->where('maintenance_out_item.name', 'like', '%'.$name.'%');
            }
            if($shop_id != ''){
                $out_list->where('maintenance_out_list.shop_id', $shop_id);
            }
            $out_list = $out_list->get()->toArray();
            if(($storage_way != 1) && !empty($out_list)){
                foreach ($out_list as $v) {
                    $temp = [
                        'storage_list_id' => $v['list_id'], //单号
                        'list_type' => '出库退回',
                        'date' => $v['date'], //日期
                        'code' => $v['code'], //商品编码
                        'name' => $v['name'], //商品名称
                        'number' => $v['number'], //数量
                        'tax_purchase_price' => $v['tax_purchase_price'], //含税进价
                        'tax_purchase_subtotal' => $v['tax_purchase_subtotal'], //含税小计
                        'tax_rate' => $v['tax_purchase_price']==0 ? 0 : ($v['tax_purchase_price'] * $v['number'])/$v['tax_purchase_subtotal'],
                        'storage_type' => $v['storage_type'], //入库方式
                        'no_tax_purchase_price' => $v['no_tax_purchase_price'], //无税进价
                        'no_tax_purchase_subtotal' => $v['no_tax_purchase_price'] * $v['number'], //无税小计
                        'pay_type' => $v['pay_type'], //付款方式
                        'type' => $v['type'], //入库类型
                        'item_type' => $v['item_type'], //耗材类型
                        'item_id' => $v['item_id'], //耗材id
                        'storage_list_type' => $v['storage_list_type'], //入库类型
                        'shop_id' => $v['shop_id'],
                        'team_id' => $v['team'],
                        'shop_name' => $v['shop_name'],
                        'real_name' => $v['real_name'],
                    ];
                    array_push($final_array, $temp);
                }
            }
            //step 2 出库退回 end
            
            //step 3 排序分页处理 start
            $count = count($final_array);
            if($count != 0){
                $date_arr = [];
                foreach ($final_array as $v) {
                    $date_arr[] = $v['date'];
                }
                array_multisort($date_arr, $final_array);
                $final_array = array_slice($final_array, ($page-1)*$page_size, $page_size);
            }
            //step 3 排序分页处理 end
            
            $response = [
                'code' => 0, 
                'msg' => 'ok',
                'result' => [
                    'count' => $count,
                    'list' => $final_array,
                ],
            ];
        }while(false);
        
        Log::info('info', array('system' => 'ass' ,'event' => '/supply/getSupplyItemList', 'input' =>Input::all() ,'output' => $response));
        return json_encode($response);
    }
    
    
    
}